#include<stdio.h>
#include<stdbool.h>
#define Min(a,b) (a)<(b)?a:b
size_t MyStrlen(const char *s)
{
    size_t count=0;
    while(*s++!='\0')
        count++;
        return count;
}


void MyStrcpy(char *dest,const char *src)
{
    while(*src!='\0')
        *dest++=*src++;
        *dest='\0';
}

void MyStrncpy(char *dest,const char *src,size_t n)
{
     int i=0;
     while (*src!='\0'&& i++<n)
         *dest++=*src++;
     *dest='\0';


}

int MyStrcmp(const char *s1,const char *s2)
{
    while(*s1==*s2&&*s1=='\0'&& *s2=='\0')
    {
       s1++;
       s2++;
    }
     return *s1-*s2;
}
/* 
int MyStrncmp(const char * s1,const char * s2,size_t n)
{
    size_t minLen=Min(Min(MyStrlen(s1),MyStrlen(s2)),n);
    while(*s1==*s2 &&minLen--!=1)
    {
        s1++;
        s2++;
        minLen--;
    }
    return *s1-*s2;
} 
*/

int MyStrncmp(const char * s1,const char * s2,size_t n)
{
     size_t minLen=Min(Min(MyStrlen(s1),MyStrlen(s2)),n);
    if(minLen==n)
    while(*s1==*s2&& minLen--!=1)
    {
        s1++;
        s2++; 
        return *s1-*s2;
    }
    
     else
     {
        return MyStrcmp (s1,s2);
     }

}
void MyStrcat(char *dest,const char *src)
{
    MyStrcpy(dest+MyStrlen(dest),src );
}
void MyStrncat(char *dest, const char *src,size_t n)
{
     MyStrncpy(dest +MyStrlen(dest),src,n);
}
char *MyStrstr( char *dest,const char *src)
{
    size_t dlen=MyStrlen(dest);
    size_t slen=MyStrlen(src);
    for(int i=0 ; i <=dlen-slen ; i++)
    {
        if(MyStrncmp( dest+i,src,slen )== 0)
        {
             return dest+i ;
        }
    }
}

bool IsHaveChar(const char *s,char c)
{
    while(*s!='\0')
    {
        if(*s==c)
        return true;
        s++;
    }
    return false;
}

char * MyStrtok(char  *src,const char * delim)
{
     static char *nextStr=NULL;//上次切割完的剩余字串；
     if (src==NULL)
        src==nextStr;//没有字符串可操作时返回空指针；
     if(src==NULL)
        return NULL;
        //跳过多余所有分割符号
     while(*src!='\0') 
    {
         if(IsHaveChar(delim,*src)==true)
         {
            src++;
         }
         else
         break;//找到第一个不是分割符的位置
    }
    if(*src=='\0')
    return NULL;
    //src：有效字符串的首地址
    char *end=src;
    while(*end='\0')
{
    if(IsHaveChar(delim,*end)==true)
        break ;
    else
    end++;
}
     if(*end=='\0')
     {
        nextStr=NULL;
     }
     else
     {
        *end='\0';
        nextStr=end+1;
     }
     return src;


}//strtok,会把分隔符变成\0
//继续找到下分隔符

int main()
{
/*     printf("%ld\n",MyStrlen("hellowrld"));
    const char *s="hello";
    char str[6]={0};
    MyStrncpy(str,s,3);
    printf("%s\n",str); */
    
    char str[]="zhangsan,lisi,wangwu";
    char *s=MyStrtok(str,",");
    while(s!=NULL)
    {
         printf("%s\n",s);
         s = MyStrtok(NULL,",");
    }
   
    return 0;
}